package cn.xd.base.controller.middle;

import cn.xd.base.application.dto.query.DictQuery;
import cn.xd.base.application.service.common.DictAppService;
import cn.xd.base.application.service.query.DictQueryAppService;
import cn.xd.base.domain.bean.command.AddDictCommand;
import cn.xd.base.domain.bean.command.AddDictGroupCommand;
import cn.xd.base.domain.bean.command.UpdateDictCommand;
import cn.xd.base.domain.bean.command.UpdateDictGroupCommand;
import cn.xd.base.domain.bean.response.DictDTO;
import cn.xd.base.domain.bean.response.DictGroupDTO;
import cn.xd.common.response.ResponseResult;
import cn.xd.web.utils.SecurityUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : mirzaLin
 * @data : 2023/11/20
 * @description : 字典交互层
 **/
@RestController
@RequestMapping("/dict")
public class DictController {
    @Resource
    private DictQueryAppService dictQueryAppService;

    @Resource
    private DictAppService dictAppService;

    /**
     * 获取字典列表分页
     *
     * @param query 查询条件：名称、编码
     * @return 数据字典（分页）
     */
    @PostMapping("page/list")
    public ResponseResult<Page<DictDTO>> getList(@RequestBody DictQuery query) {
        return ResponseResult.success(dictQueryAppService.getDictPage(query));
    }

    /**
     * 获取字典编码查询字典信息
     *
     * @param code 编码
     * @return 数据字典（分页）
     */
    @PostMapping("getDictByCode/{code}")
    public ResponseResult<DictDTO> getDictByCode(@PathVariable String code) {
        DictDTO dictDto = dictQueryAppService.getDictByCode(code);
        return ResponseResult.success(dictDto);
    }

    /**
     * 获取字典分组列表
     *
     * @return 数据字典分组
     */
    @PostMapping("group/list")
    public ResponseResult<List<DictGroupDTO>> getList() {
        List<DictGroupDTO> dictGroup = dictQueryAppService.getDictGroup();
        return ResponseResult.success(dictGroup);
    }

    /**
     * 新增字典分组
     *
     * @param command 新增操作
     * @return 新增的字典
     */
    @PostMapping("group/add")
    public ResponseResult<?> addDictGroup(@Validated @RequestBody AddDictGroupCommand command) {
        dictAppService.addDictGroup(command, SecurityUtils.getUserId());
        return ResponseResult.success();
    }

    /**
     * 新增字典
     *
     * @param command 新增操作
     * @return 新增的字典
     */
    @PostMapping("add")
    public ResponseResult<?> addDict(@Validated @RequestBody AddDictCommand command) {
        DictDTO dictDTO = dictAppService.addDict(command, SecurityUtils.getUserId());
        return ResponseResult.success();
    }


    /**
     * 修改字典分组
     *
     * @param command 修改操作
     * @return 修改后的字典
     */
    @PostMapping("group/update")
    public ResponseResult<?> updateDictGroup(@Validated @RequestBody UpdateDictGroupCommand command) {
        dictAppService.updateDictGroup(command, SecurityUtils.getUserId());
        return ResponseResult.success();
    }

    /**
     * 修改字典
     *
     * @param command 修改操作
     * @return 修改后的字典
     */
    @PostMapping("update")
    public ResponseResult<?> updateDict(@Validated @RequestBody UpdateDictCommand command) {
        DictDTO dictDTO = dictAppService.updateDict(command, SecurityUtils.getUserId());
        return ResponseResult.success();
    }

    /**
     * 删除字典
     *
     * @param dictCode 字典编码
     * @return 新增的字典
     */
    @PostMapping("delete/{dictCode}")
    public ResponseResult<?> deleteDict(@PathVariable String dictCode) {
        dictAppService.deleteDict(dictCode);
        return ResponseResult.success();
    }


}
